
export default {

  /**
   *@describe 获取系统当前时间  年月日时分秒
   @param {用于分割当前时间} slice 
   @optional {'date','tiem'}
   * @return 2021-10-11 14:58:02
   */
  getData (slice) {

    let date = new Date(),
      seperator1 = "-",
      seperator2 = ":",
      month = date.getMonth() + 1,
      strDate = date.getDate(),
      hour = date.getHours(),
      minute = date.getMinutes(),
      second = date.getSeconds();

    month = (month >= 1 && month <= 9) ? "0" + month : month;
    strDate = strDate >= 0 && strDate <= 9 ? strDate = "0" + strDate : strDate;
    hour = hour < 10 ? hour = "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? second = "0" + second : second

    let currentdate =
      String(date.getFullYear()) +
      String(seperator1) +
      String(month) +
      String(seperator1) +
      String(strDate);

    let current = String(hour) +
      String(seperator2) +
      String(minute) +
      String(seperator2) +
      String(second);

    return slice === 'date' ? currentdate : slice === 'tiem' ? current : `${currentdate} ${current}`;
  },

  /**
   * 传入时间格式为yyyyMMddhhmmss 返回格式化时间为yyyy-MM-DD HH:mm:ss格式
   * @param {string and number} time 
   *  * @param {string} Division 
   * @returns string
   */
  formatTime (time, Division = "-") {
    if (time == undefined)
    {
      throw new Error('time 参数必传')
    }

    time = String(time);
    return (
      time.slice(0, 4) +
      `${Division}` +
      time.slice(4, 6) +
      `${Division}` +
      time.slice(6, 8) +
      " " +
      time.slice(8, 10) +
      ":" +
      time.slice(10, 12) +
      ":" +
      time.slice(12, 14)
    );
  },

  /**
   * 给秒 返回 小时-分钟-秒
   * @param {number} value 
   * @returns 
   */
  secondsConversion (value) {
    let result = parseInt(value);
    let h =
      Math.floor(result / 3600) < 10 ?
        "0" + Math.floor(result / 3600) :
        Math.floor(result / 3600);
    let m =
      Math.floor((result / 60) % 60) < 10 ?
        "0" + Math.floor((result / 60) % 60) :
        Math.floor((result / 60) % 60);
    let s =
      Math.floor(result % 60) < 10 ?
        "0" + Math.floor(result % 60) :
        Math.floor(result % 60);

    let res = "";
    if (h !== "00") res += `${h}h`;
    if (m !== "00") res += `${m}min`;
    res += `${s}s`;
    return res;
  },

  // 给秒 返回 小时和秒
  s_to_hs (s) {
    //计算分钟
    //算法：将秒数除以60，然后下舍入，既得到分钟数
    var h;
    h = Math.floor(s / 60);
    //计算秒
    //算法：取得秒%60的余数，既得到秒数
    s = s % 60;
    //将变量转换为字符串
    h += "";
    s += "";
    //如果只有一位数，前面增加一个0
    h = h.length == 1 ? "0" + h : h;
    s = s.length == 1 ? "0" + s : s;
    return h + ":" + s;
  },

  /**
   * 比较二个时间相差
   * @param startTime 对比开始时间
   * @param endTime    对比结束时间
   * @param diffType 返回对比类型参数  second 秒   minute分  hour 小时  day 天
   * @returns {number} 返回 设置的 秒 分 小时 天
   * @constructor
   */
  getDateDiff (startTime, endTime, diffType) {
    //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
    startTime = startTime.replace(/-/g, "/");
    endTime = endTime.replace(/-/g, "/");

    //将计算间隔类性字符转换为小写
    diffType = diffType.toLowerCase();
    var sTime = new Date(startTime); //开始时间
    var eTime = new Date(endTime); //结束时间
    //作为除数的数字
    var divNum = 1;
    switch (diffType)
    {
      case "second": //秒
        divNum = 1000;
        break; // 分
      case "minute":
        divNum = 1000 * 60;
        break;
      case "hour": // 小时
        divNum = 1000 * 3600;
        break;
      case "day": // 天
        divNum = 1000 * 3600 * 24;
        break;
      default:
        break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
  },

  /**
   * 获取当前天的周一到周末的年月日
   * @returns ['2021/10/11', '2021/10/12', '2021/10/13', '2021/10/14', '2021/10/15', '2021/10/16', '2021/10/17']
   */
  weekTime () {
    var newDate = new Date();
    var timesStamp = newDate.getTime(); // 时间戳
    var currenDay = newDate.getDay();
    var dates = [];
    for (var i = 0; i < 7; i++)
    {
      dates.push(
        new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
          .toLocaleDateString()
          .replace(/[年月]/g, "-")
          .replace(/[日上下午]/g, "")
      );
    }
    return dates;
  },
  /**
   * 获取本周周一年月日
   * @returns 
   */
  monday () {
    var now = new Date();
    var nowTime = now.getTime();
    var day = now.getDay();
    var oneDayTime = 24 * 60 * 60 * 1000;
    var MondayTime = nowTime - (day - 1) * oneDayTime; // 显示周一
    // var SundayTime =  nowTime + (7-day)*oneDayTime ;//显示周日
    function p (s) {
      return s < 10 ? "0" + s : s;
    }
    const d = new Date(MondayTime);
    const begintime = `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`

    return begintime;
  },
  /**
   * 获取本周日 的时间
   * @returns 
   */
  sunday () {
    var now = new Date();
    var nowTime = now.getTime();
    var day = now.getDay();
    var oneDayTime = 24 * 60 * 60 * 1000;
    // var MondayTime = nowTime - (day - 1) * oneDayTime // 显示周一
    var SundayTime = nowTime + (7 - day) * oneDayTime; // 显示周日
    function p (s) {
      return s < 10 ? "0" + s : s;
    }
    const d = new Date(SundayTime);
    // const resTime = p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
    const resDate = `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`
    return resDate;
  },
  /**
   * 获取本月的第一天
   * @returns 
   */
  getCurrentMonthFirst () {
    var date = new Date();
    date.setDate(1);
    var month = String(date.getMonth() + 1);
    var day = date.getDate();
    month = month <= 9 ? "0" + month : month;
    day = day <= 9 ? "0" + day : day
    return `${date.getFullYear()}-${month} ${day}`
  },
  /**
   * 
   * @returns 获取本月的最后一天
   */
  getCurrentMonthLast () {
    var mydate = new Date();
    var currentMonth = mydate.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(mydate.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    var lastTime = new Date(nextMonthFirstDay - oneDay);
    var month = parseInt(lastTime.getMonth() + 1);
    var day = lastTime.getDate();
    month = month <= 9 ? "0" + month : month;
    day = day <= 9 ? "0" + day : day
    var date = new Date(
      String(mydate.getFullYear()) + "-" + String(month) + "-" + String(day)
    );
    if (date.getMonth() + 1 <= 9)
    {
      var yue = "0" + (date.getMonth() + 1);
    } else
    {
      yue = date.getMonth() + 1;
    }

    return `${date.getFullYear()}-${yue}-${date.getDate()}`;
  },

  /**
   * 获取周几
   * @returns string
   */
  getWeekDate () {
    let now = new Date(),
      day = now.getDay(),
      weeks = new Array(
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六"
      );
    var week = weeks[day];
    return week;
  },
  /**
 *
 * @param {格式化中国标准时间} d
 * @param 返回yyyyMMddhhmmss
 *
 */
  timeFormatsss (d) {
    d = new Date(new Date(new Date().setHours(0, 0, 0, 0)));
    let getMonth = d.getMonth() + 1;
    let getDate = d.getDate();
    let getHours = d.getHours();
    let getMinutes = d.getMinutes();
    let getSeconds = d.getSeconds();
    if (d.getMonth() + 1 >= 0 && d.getMonth() + 1 <= 9)
    {
      getMonth = "0" + getMonth;
    }
    if (getDate >= 0 && getDate <= 9)
    {
      getDate = "0" + getDate;
    }
    if (getHours >= 0 && getHours <= 9)
    {
      getHours = "0" + getHours;
    }
    if (getMinutes >= 0 && getMinutes <= 9)
    {
      getMinutes = "0" + getMinutes;
    }
    if (getSeconds >= 0 && getSeconds <= 9)
    {
      getSeconds = "0" + getSeconds;
    }
    const newDate = `${d.getFullYear()}-${getMonth}-${getDate} ${getHours}:${getMinutes}：${getSeconds}`
    return newDate;
  }
};